﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyHocPhi.DAL;
using QuanLyHocPhi.Entity;
using QuanLyHocPhi.Util;

namespace QuanLyHocPhi
{
    public partial class frmLophoc : DevComponents.DotNetBar.Metro.MetroForm
    {
        DOI_TUONG_DAO dtDao = new DOI_TUONG_DAO();
        LOP_HOC_DAO lhDao = new LOP_HOC_DAO();
        LOP_HOC lh = new LOP_HOC();
        IList<LOP_HOC> ds = null;
        public int MaLopHoc { get; set; }
        public frmLophoc(int maLophoc)
        {
            InitializeComponent();
            this.MaLopHoc = maLophoc;
        }

        public frmLophoc()
        {
            InitializeComponent();
        }

       

        private void frmLophoc_Load(object sender, EventArgs e)
        {
           // cbbLoailop.Items;
            IList<DOI_TUONG> dsDoiTuong = dtDao.GetObjectsByLOP("class_type");
            foreach (DOI_TUONG dt in dsDoiTuong)
            {
                cbbLoailop.Items.Add(dt);
                
            }
            ds = lhDao.GetAll("%"); 
            dtgvLophoc.AutoGenerateColumns = false;
            dtgvLophoc.DataSource = ds;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            int err = 0;
            if (cbbLoailop.Text == "")
            {
                errorProvider1.SetError(cbbLoailop, "Yêu cầu chọn loại lớp học");
                return;
            }
            lh.MA_LOAI_LOP = new DOI_TUONG() { MA_DOI_TUONG = ((DOI_TUONG)cbbLoailop.SelectedItem).MA_DOI_TUONG };
            lh.NGAY_BD = dtpNgaybd.Value;
            lh.NGAY_KT = dtpNgaykt.Value;
            lh.CA_HOC = txtCahoc.Text;
            lh.TEN_LOP_HOC = txtTenlop.Text;

            lhDao.Insert(lh);
            frmLophoc_Load(sender, e);

        }

        private void dtgvLophoc_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dtgvLophoc_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //cbbLoailop.SelectedText = ds[e.RowIndex].ToString();
            dtpNgaybd.Value = ds[e.RowIndex].NGAY_BD;
            dtpNgaykt.Value = ds[e.RowIndex].NGAY_BD;
            txtTenlop.Text = ds[e.RowIndex].ToString();
            //txtCahoc.Text = ds[e.RowIndex].ToString() ;
            lh = ds[e.RowIndex];
            dtpNgaykt.Enabled = false;

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
           // lh.MA_LOAI_LOP = new DOI_TUONG() { MA_DOI_TUONG = ((DOI_TUONG)cbbLoailop.SelectedItem).MA_DOI_TUONG };
            lh.NGAY_BD = dtpNgaybd.Value;
            lh.NGAY_KT = dtpNgaykt.Value;
            lh.CA_HOC = txtCahoc.Text;
            lh.TEN_LOP_HOC = txtTenlop.Text;

            lhDao.Update(lh);
            frmLophoc_Load(sender, e);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            lhDao.Delete(lh.MA_LOP_HOC);
            frmLophoc_Load(sender, e);
            txtCahoc.Text = "";
            txtTenlop.Text = "";
            
            
           
        }
        
        private void txtTen_TextChanged(object sender, EventArgs e)
        {
            
            
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}